﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Effects;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Win2dEffectControlExhibitPage : Page
    {

        CanvasBitmap cBitmap;

        public Win2dEffectControlExhibitPage()
        {
            this.InitializeComponent();
        }

       
        private async void selectPicture_Click(object sender, RoutedEventArgs e)
        {
            cBitmap = await Function.SelectImage(canvas.Device);
        }

        private void canvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
        {
            
            
            //if (cBitmap == null)
            //    return;
            //var effect =() displacement.Effect;
            //var turblen = (TurbulenceEffect)turbulence.Effect;
            //effect.Source = cBitmap;
            //effect.Displacement = turblen;

            //using (var ds = args.DrawingSession)
            //{
            //    ds.DrawImage(turblen);

            //    ds.DrawImage(effect);
            //}
            ////DrawAlphaMaskEffect(args);

        }

        private void DrawAlphaMaskEffect(CanvasDrawEventArgs resource)
        {


            using (var ds = resource.DrawingSession)
            {
                var cl = new CanvasCommandList(resource.DrawingSession.Device);
                using(var cs = cl.CreateDrawingSession())
                {
                    cs.FillCircle(100, 100, 50, Colors.Red);
                    cs.FillRectangle(new Rect(0, 0, canvas.ActualWidth, canvas.ActualHeight), Colors.Red);
                }

                OpacityEffect oe = new OpacityEffect()
                {
                    Opacity = 1f,
                     Source = cl,

                };

                AlphaMaskEffect am = new AlphaMaskEffect() {
                    AlphaMask = oe,
                    Source = cBitmap,
                
                };

                


                ds.DrawImage(am);
            }

        }

        private void tranform_Transform2DEffectChange(win2dEffectControl.Transform2DEffectControl sender, Microsoft.Graphics.Canvas.Effects.Transform2DEffect value)
        {
            canvas.Invalidate();
        }

        private void border_EffectChanged(object sender)
        {
            canvas.Invalidate();
        }
    }
}
